
#产生交易信号
trade.signal<-function(test.date, final.date, Stock.test.sta, Stock.inter.list, idx.inter, cash=100000){       # traderecord(n) 返回一个list， 里面为Stocklist中第n支股票的每日交易情况和仓位情况     
  names(Stock.inter.list) = as.integer(names(Stock.inter.list))
  all.date = rownames(Stock.test.sta)
  Stock.symbol = colnames(Stock.test.sta)
  #Stock.symbol = c("000008", "000009")
  Stock.intra.test.date = all.date[(all.date>test.date)]
  Stock.intra.test.date = all.date[(all.date<final.date)]
  
  equity<-cash; #初始化资产
  Date.length<-length(Stock.intra.test.date) #测试的时长
  buy.signal<-vector(length=Date.length);  
  Stock.pos<- vector(length=Date.length);
  Stock.pos.symbol<- vector(length=Date.length);
  
  Stock.share<- vector(length=Date.length); #股票持有量
  equity.stock<- vector(length=Date.length);
  equity.cash<- vector(length=Date.length);
  equity.all <- vector(length=Date.length);
  
  Stock.fluc.mean <- vector(length=Date.length);
  Stock.width.mean <- vector(length=Date.length);
  idx.fluc.mean <-vector(length=Date.length);
  idx.width.mean <- vector(length=Date.length);
  idx.share <- vector(length=Date.length);
  equity.idx <- vector(length=Date.length);
  
  neg.days.sum.vec <- vector(length=Date.length);
  
  #初始化资金
  start.date = 12 
  equity.cash[1] = cash
  equity.cash[start.date] = cash
  Stock.share[start.date] = 0
  Stock.pos[start.date] = 0
  Stock.pos.symbol[start.date] = Stock.symbol[1]
  equity.all[start.date] = cash
  
  last.hold = 1
  
  for(i in start.date:(Date.length-1)){  #从第二日开始计算
    ##print(i);
    #print(as.character(Stock.intra.test.date[i]))
    #统计日内数据特性
    #Stock.wave.oneday = Stock.test.sta[[as.character(Stock.intra.test.date[i])]]
    #Stock.wave.oneday.fluc.mean<-Stock.wave.oneday$fluc.mean;
    #Stock.wave.oneday.width.mean<-Stock.wave.oneday$width.mean;
    
    #if(Stock.wave.oneday.fluc.mean>Trig.fluc.pos && Stock.wave.oneday.width.mean>Trig.width) #买入
    #  buy.signal[i] = 1  else if(Stock.wave.oneday.fluc.mean<Trig.fluc.neg && Stock.wave.oneday.width.mean>Trig.width) #卖出
    #  buy.signal[i] = -1 else
    #  buy.signal[i] = 0; 
    
    #if(Stock.wave.oneday.fluc.mean>Stock.his.sta$Trig.fluc.pos) #股价非正常上涨，买入
    #  buy.signal[i] = 1  else if(Stock.wave.oneday.fluc.mean<Stock.his.sta$Trig.fluc.neg) #股价非正常下跌，卖出
    #    buy.signal[i] = -1 else
    #      buy.signal[i] = 0; 
    
    #if(Stock.wave.oneday.fluc.mean<Stock.his.sta$Norm.fluc && Stock.wave.oneday.width.mean<Stock.his.sta$Trig.width) #股价趋于平稳
    #  buy.signal[i] = -1
    
    Date = as.character(Stock.intra.test.date[i])
    Date.yes = as.character(Stock.intra.test.date[i-10])
    ratio = Stock.test.sta[Date,]-Stock.test.sta[Date.yes,] #选择涨幅波动最大的
    ratio[is.na(ratio)] = 0  #删除NA的
    ratio[is.infinite(ratio)] = 0  #删除inf的
    ratio[Stock.test.sta[Date,]<0] = 0 #删除当前波动小于0
    if(max(ratio)>0)
      buy.signal[i] =1
    else
      buy.signal[i] =-1
    
    #print(max(ratio))
    sel.symbol = Stock.symbol[which.max(ratio)]
    
    if(max(ratio)<=0) #没有可买的股票
      buy.signal[i] =-1
    
    Stock.fluc.mean[i] = max(ratio)
    
    ##根据ema12判断
    #if(Stock.test.sta[[as.character(Stock.intra.test.date[i])]]$fluc.mean>Stock.test.sta[[as.character(Stock.intra.test.date[i-1])]]$fluc.mean)
    #{
    #  buy.signal[i] = 1
    #}
    #else
    #  buy.signal[i] = -1
    #
    ##股价下跌
    #if(Stock.test.sta[[as.character(Stock.intra.test.date[i])]]$fluc.mean<0)
    #  buy.signal[i] = -1
    #
    #neg.days.sum = 0
    #if(i>8) #股价连续下跌的情况
    #{
    #  # 5 - 4
    #  days.sel = c(Stock.test.sta[[as.character(Stock.intra.test.date[i-1])]]$fluc.mean, Stock.test.sta[[as.character(Stock.intra.test.date[i-2])]]$fluc.mean, Stock.test.sta[[as.character(Stock.intra.test.date[i-3])]]$fluc.mean, Stock.test.sta[[as.character(Stock.intra.test.date[i-4])]]$fluc.mean, Stock.test.sta[[as.character(Stock.intra.test.date[i-5])]]$fluc.mean, Stock.test.sta[[as.character(Stock.intra.test.date[i-6])]]$fluc.mean)
    #  neg.days.sum <- sum(days.sel<0)
    #  if(neg.days.sum>=5)
    #    buy.signal[i] = -1
    #}
    #只根据阈值判断
    #if(Stock.wave.oneday.fluc.mean>Stock.his.sta$Trig.fluc.pos) 
    #  buy.signal[i] = 1
    #else
    #  buy.signal[i] = -1
    
    #print(idx.wave.oneday.mean);
    #print(idx.sta[[as.character(as.Date(Stock.intra.test.date[i],"%Y%m%d"))]]$mean);
    #print(Stock.wave.oneday.fluc.mean)
    
    #print(Stock.intra.test.date[i])
    #print(idx.sta[[as.character(Stock.intra.test.date[i])]]$fluc.mean)
    #缺失当天的指数数据, 用上一日填充
    if(is.null(idx.sta[[as.character(Stock.intra.test.date[i])]]$fluc.mean))
    {
      idx.sta[[as.character(Stock.intra.test.date[i])]]$fluc.mean = idx.sta[[as.character(Stock.intra.test.date[i-1])]]$fluc.mean;  #设置为很大，不交易
      idx.sta[[as.character(Stock.intra.test.date[i])]]$width.mean = idx.sta[[as.character(Stock.intra.test.date[i-1])]]$width.mean;  #设置为很大，不交易
    }
    
    #与大盘指数波动对比
    #正向波动好于大盘，那么就买入，否则卖出
    #if(Stock.wave.oneday.fluc.mean>idx.sta[[as.character(Stock.intra.test.date[i])]]$fluc.mean) 
    #{#buy.signal[i] = 1
    #}else 
    #{}#buy.signal[i] = -1
    
    #记录统计数据
    #Stock.fluc.mean[i] <- Stock.wave.oneday.fluc.mean
    #Stock.width.mean[i] <- Stock.wave.oneday.width.mean
    #idx.fluc.mean[i] <-idx.sta[[as.character(Stock.intra.test.date[i])]]$fluc.mean
    #idx.width.mean[i] <- idx.sta[[as.character(Stock.intra.test.date[i])]]$width.mean
    #neg.days.sum.vec[i] <- neg.days.sum
    
    hold = 1 #是否继续持有，若股票停牌，则必须继续持有
    if(Date%in%Stock.inter.list[[Stock.pos.symbol[i]]]$time)
      hold = 0
    
    if(Stock.pos[i]==0)
      hold = 0
    
	if(buy.signal[i]==0)
	  hold = 1
	  
    idx.inter.close = idx.inter[idx.inter$time==as.character(Stock.intra.test.date[i]),]$CLOSE
    
	if(i<last.hold+3)  #买入后至少持有3日
		hold = 1
		
    #原则：
    #只能用equity.cash买股票
    #卖空股指的钱计入equity.cash中
    #卖出后再买入股票，需要先计算卖出股票、买入股指后的equity.cash
    if(hold==0 && buy.signal[i]==1 && Stock.pos[i]==0) #当日收盘价买入
    {
	  last.hold = i
	  
	  Stock.width.mean[i] = "buy"
      print('pos1')
      Stock.inter.close = Stock.inter.list[[sel.symbol]][Stock.inter.list[[sel.symbol]]$time==Date,]$CLOSE #收盘价
      print(Stock.inter.close)
      
      Stock.pos[i+1] = 1
      Stock.pos.symbol[i+1] = sel.symbol
      Stock.share[i+1] = ifelse(floor(equity.cash[i]/Stock.inter.close)<=0, 0, floor(equity.cash[i]/Stock.inter.close)) #持有股票量
      idx.share[i+1] = floor(Stock.share[i+1]*Stock.inter.close/idx.inter.close) #卖空沪深300 份数
      equity.cash[i+1] = equity.cash[i]- Stock.share[i+1]*Stock.inter.close + idx.share[i+1]*idx.inter.close   #现金
      
	  equity.idx[i] = idx.share[i]*idx.inter.close #卖空的现金
      equity.stock[i] = 0 #股票市值
	  
      equity.all[i] = equity.stock[i]+equity.cash[i]- idx.share[i]*idx.inter.close #总资产，不包含卖空指数的钱
    }  else if(hold==0 && buy.signal[i]==-1 && Stock.pos[i]==1) #当日收盘价卖出
    {
	  
      print('pos2')
	  Stock.width.mean[i] = "sell"
	  
      Stock.inter.close = Stock.inter.list[[Stock.pos.symbol[i]]][Stock.inter.list[[Stock.pos.symbol[i]]]$time==Date,]$CLOSE
      print(Stock.inter.close)
      
      Stock.pos[i+1]=0
      Stock.pos.symbol[i+1] = 0
      Stock.share[i+1] = 0
      idx.share[i+1] =idx.share[i]*idx.inter.close #买入沪深300 份数
      
      equity.idx[i+1] =  idx.share[i+1]*idx.inter.close #买入股指
	  equity.cash[i+1] = equity.cash[i]+ Stock.share[i]*Stock.inter.close - idx.share[i]*idx.inter.close   #现金
	  
      equity.idx[i] = idx.share[i]*idx.inter.close #卖空的现金
      equity.stock[i] = Stock.inter.close*Stock.share[i] #股票市值
	  
      equity.all[i] = equity.stock[i]+equity.cash[i]- idx.share[i]*idx.inter.close #总资产，不包含卖空指数的钱
    }    else if(hold==0 && buy.signal[i]==1 && Stock.pos[i]==1 && Stock.pos.symbol[i]!=sel.symbol && Stock.pos.symbol[i]!=0) #卖出原有股票，买入另一只股票
    {
	  last.hold = i
	  Stock.width.mean[i] = "buy2"
	  
      print('pos3')
      Stock.inter.close.original = Stock.inter.list[[Stock.pos.symbol[i]]][Stock.inter.list[[Stock.pos.symbol[i]]]$time==Date,]$CLOSE
      Stock.inter.close          = Stock.inter.list[[sel.symbol]][Stock.inter.list[[sel.symbol]]$time==Date,]$CLOSE
      
      Stock.pos[i+1]=1
      Stock.pos.symbol[i+1] = sel.symbol
      
      equity.cash[i+1] = equity.cash[i] + Stock.inter.close.original*Stock.share[i] - idx.share[i]*idx.inter.close  #卖原股票
      Stock.share[i+1] = ifelse(floor( equity.cash[i+1]/ Stock.inter.close)<=0, 0, floor( equity.cash[i+1]/ Stock.inter.close))
	  idx.share[i+1] = floor(Stock.share[i+1]*Stock.inter.close/idx.inter.close) #卖空沪深300 份数	  
      equity.cash[i+1] = equity.cash[i+1] - Stock.inter.close*Stock.share[i+1] + idx.share[i+1]*idx.inter.close #买新股票
	  
      equity.idx[i] = idx.share[i]*idx.inter.close #卖空的现金      
      equity.stock[i] =  Stock.inter.close.original*Stock.share[i]
	  
      equity.all[i] = equity.stock[i]+equity.cash[i]- idx.share[i]*idx.inter.close #总资产，不包含卖空指数的钱
    }   else if(hold==0 && Stock.pos.symbol[i]==0)  #不持有任何股票, 也不买入
    {
      hold = 1
    }  
    else 
    {
      hold = 1
    }
    
    #持有股票，但是不买入也不卖出
    if(hold)
    {
	  Stock.width.mean[i] = "hold"
      print('pos5')
      if(Date%in%Stock.inter.list[[Stock.pos.symbol[i]]]$time) #股票当日不停盘
        Stock.inter.close = Stock.inter.list[[Stock.pos.symbol[i]]][Stock.inter.list[[Stock.pos.symbol[i]]]$time==Date,]$CLOSE
      else #股票停盘
        Stock.inter.close = NA
      Stock.pos[i+1]=Stock.pos[i];  
      Stock.pos.symbol[i+1]=Stock.pos.symbol[i];  
      equity.cash[i+1] = equity.cash[i]
      Stock.share[i+1] = Stock.share[i] 
      
      idx.share[i+1] = idx.share[i] #买入沪深300 份数
	  
      equity.idx[i] = idx.share[i]*idx.inter.close #卖空的现金      
      equity.stock[i] = Stock.share[i]*Stock.inter.close
	  
      equity.all[i] = equity.stock[i]+equity.cash[i]- idx.share[i]*idx.inter.close #总资产，不包含卖空指数的钱
    }
  }
  #最后一日卖出
  buy.signal[i]=-1;
  Stock.pos[i]=0;
  
  summary = list(date= Stock.intra.test.date, buy.signal=buy.signal, Stock.pos=Stock.pos, Stock.pos.symbol= Stock.pos.symbol, Stock.share=Stock.share, equity.cash=equity.cash, equity.idx=equity.idx, equity.stock=equity.stock, equity.all=equity.all, Stock.fluc.mean= Stock.fluc.mean, Stock.width.mean=Stock.width.mean, idx.fluc.mean= idx.fluc.mean, idx.width.mean=idx.width.mean, neg.days.sum.vec = neg.days.sum.vec, idx.share=idx.share);
  return(summary);  
}


